<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 下面就是树形控件内容了 -->
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="add-dept" :tree-node="node" @addDepts="getDepartments" />
  </div>
  <!-- 放置新增弹层组件 -->
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '../../api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept'
export default {
  name: 'Departments',
  components: { treeTools, AddDept },
  data() {
    return {
      showDialog: false, // 显示窗体
      node: {}, // 点击行的数据
      defaultProps: {
        label: 'name'
      },
      departs: [
        {
          name: '总裁办',
          manager: '曹操',
          children: [{ name: '董事会', manager: '曹丕' }]
        },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '') // 需要将其转为树形结构
      // console.log(result)  所有跟pid 为 空
    },
    addDepts(node) {
      // console.log(node)
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node
      this.$refs['add-dept'].showDialog = true //
    },
    editDepts(node) {
      // console.log(node)
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node
      this.$refs['add-dept'].showDialog = true //
      this.$refs['add-dept'].getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
